package com.shengyijie.activity;
 
import com.shengyijie.activity.tab.CategoryActivity;
import com.shengyijie.activity.tab.HomeActivity; 
import com.shengyijie.activity.tab.MoreActivity;
import com.shengyijie.activity.tab.RecommendedActivity; 
import com.shengyijie.activity.tab.user.UserActivity;
import com.shengyijie.context.ContextApplication;   

import android.app.ActivityGroup;
import android.app.AlertDialog;  
import android.content.DialogInterface;
import android.content.Intent;  
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;  
import android.util.DisplayMetrics;
import android.view.KeyEvent; 
import android.view.View;
import android.view.Window; 
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TextView;



public class MainActivity extends  ActivityGroup implements OnTabChangeListener{

	 public static MainActivity group;
	 Uri originalUri ;
	 public static TabHost tabhost;
	 public static TabHost.TabSpec tabOne;
	 public static TabHost.TabSpec tabTwo;
	 public static TabHost.TabSpec tabThr;
	 public static TabHost.TabSpec tabFour;
	 public static TabHost.TabSpec tabFive;  
	 LinearLayout ll;
	 private ContextApplication app;
	 public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); 
	        setContentView(R.layout.main);
	        group=this;
	        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,     
                    R.layout.title);
	        app=(ContextApplication)this.getApplication();
	        ContextApplication.activity=this;
        	app.right= (Button) MainActivity.this.findViewById(R.id.right);       	
	        tabhost=(TabHost) this.findViewById(R.id.tabhost);
	        tabhost.setup(this.getLocalActivityManager());
	        tabhost.setBackgroundColor(Color.argb(150, 22, 70, 150));  
	        
	   	    tabOne=tabhost.newTabSpec("home").setIndicator("").
	        	/*	setIndicator("",getResources().getDrawable(R.drawable.around)).*/
	        		setContent(new Intent(this, HomeActivity.class));
	        tabTwo=tabhost.newTabSpec("category").setIndicator("").
	               /* setIndicator("",getResources().getDrawable(R.drawable.mycoupon)).*/
	        		setContent(new Intent(this, CategoryActivity.class)); 
	        tabThr=tabhost.newTabSpec("recommended").setIndicator("").
	                /*setIndicator("",getResources().getDrawable(R.drawable.other)).*/
	        		setContent(new Intent(this, RecommendedActivity.class));
	        tabFour=tabhost.newTabSpec("user").setIndicator("").
	               /*  setIndicator("",getResources().getDrawable(R.drawable.set)).*/
	        		setContent(new Intent(this, UserActivity.class));
	        tabFive=tabhost.newTabSpec("more").setIndicator("").
		               /*  setIndicator("",getResources().getDrawable(R.drawable.set)).*/
		        		setContent(new Intent(this, MoreActivity.class));
	        tabhost.addTab(tabOne);
	        tabhost.addTab(tabTwo);
	        tabhost.addTab(tabThr);
	        tabhost.addTab(tabFour);
	        tabhost.addTab(tabFive);
	        for(int i=0;i<5;i++){
	            View view;
	            view = tabhost.getTabWidget().getChildAt(0);
	        	view.setBackgroundResource(R.drawable.tab_home_selector);
	        	view = tabhost.getTabWidget().getChildAt(1);
	        	view.setBackgroundResource(R.drawable.tab_category_selector);
	        	view = tabhost.getTabWidget().getChildAt(2);
	        	view.setBackgroundResource(R.drawable.tab_recommended_selector);
	        	view = tabhost.getTabWidget().getChildAt(3);
	        	view.setBackgroundResource(R.drawable.tab_user_selector);
	        	view = tabhost.getTabWidget().getChildAt(4);
	        	view.setBackgroundResource(R.drawable.tab_more_selector);
		     
	        }

	        DisplayMetrics dm = new DisplayMetrics();   
	        getWindowManager().getDefaultDisplay().getMetrics(dm);  
	        int height;
	        if(dm.densityDpi==DisplayMetrics.DENSITY_LOW){
	        	height = dm.heightPixels/9+4;
	        	tabhost.setPadding(0,0, 0, tabhost.getPaddingBottom()-4);
	        }else{
	        	tabhost.setPadding(0,0, 0, tabhost.getPaddingBottom()-5);
	        	height = dm.heightPixels/12+8;
	        }
	        for (int i =0; i < tabhost.getTabWidget().getChildCount(); i++) {
	            
	            tabhost.getTabWidget().getChildAt(i).getLayoutParams().height = height;
	       
	        }
            tabhost.setCurrentTab(0);
            tabhost.setOnTabChangedListener(this);  
	 }
	  
	 @Override  
	 public boolean onKeyDown(int keyCode,KeyEvent event) {   
	    if (keyCode == KeyEvent.KEYCODE_BACK) { 
			 new AlertDialog.Builder(MainActivity.this).setTitle("提示")
			 .setMessage("确定退出吗？")
			 .setPositiveButton("确定", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int whichButton) {
					android.os.Process.killProcess(android.os.Process.myPid());
				}
			  })
			  .setNegativeButton("取消", new DialogInterface.OnClickListener() {
				    public void onClick(DialogInterface dialog, int whichButton) {
				     }
			  })
			  .show();
	         return true;   
	     } else
	         return super.onKeyDown(keyCode,event);   
	 }

	public void onTabChanged(String tabId) {	 
    	TextView title=(TextView) MainActivity.this.findViewById(R.id.title); 
    	
    	if(tabId.equals("home")){
    		ContextApplication.prePage=ContextApplication.PAGE_HOME;
    		title.setText("首页");
        }
        if(tabId.equals("category")){
        	ContextApplication.prePage=ContextApplication.PAGE_CATEGORY;
        	title.setText("分类浏览");
        	 
        }
        if(tabId.equals("recommended")){
        	 ContextApplication.prePage=ContextApplication.PAGE_RECOMMENDED;
        	 app.right.setVisibility(View.INVISIBLE);
          	 title.setText("推荐项目"); 
        }
        if(tabId.equals("user")){
        	ContextApplication.prePage=ContextApplication.PAGE_USER;
        	app.right.setVisibility(View.INVISIBLE);
           	title.setText("个人中心");
           	 
        }
        if(tabId.equals("more")){
        	 ContextApplication.prePage=ContextApplication.PAGE_MORE;
        	 app.right.setVisibility(View.INVISIBLE);
          	 title.setText("更多"); 
       }
    }    
	 
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 1) { 
            if(resultCode == RESULT_OK){ 
            } 
        }
        
    }

	 protected void onDestroy() {	  
	       super.onDestroy(); 
	 }	
	 
 
	 
}

	    
